package com.miukoo.aikao.ucenter.controller.v1;

import com.miukoo.aikao.core.dtos.*;
import com.miukoo.aikao.core.utils.ApiUtils;
import com.miukoo.aikao.core.utils.AppUserUtils;
import com.miukoo.aikao.ucenter.dto.WrongQuestionDto;
import com.miukoo.aikao.ucenter.pojos.WrongQuestion;
import com.miukoo.aikao.ucenter.services.WrongQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* <p>
* 错题本控制器
* </p>
*
* @author 聪明笨狗
* @since 2020-05-27 17:56
*/
@Api(tags={"错题集合"})
@RestController
@RequestMapping("/ucenter/wquestion/v1")
public class WrongQuestionController {

    @Autowired
    private WrongQuestionService wrongQuestionService;


    /**
    * 批量删除
    * @param reqDto
    * @return
    */
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/delete", method = { RequestMethod.POST})
    public ApiResult delete(@RequestBody BaseIdsReqDto reqDto) {
        //根据ID删除
        wrongQuestionService.removeByIds(reqDto.getIds());
        return ApiUtils.success();
    }

    /**
    * 分页查找
    * @param reqDto
    * @return
    */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/list", method = { RequestMethod.POST})
    public ApiResult<PageResDto<List<WrongQuestion>>> paging(@RequestBody PageReqDto<WrongQuestionDto> reqDto) {

        Long userId = AppUserUtils.getUser().getId();
        //分页查询并转换
        PageResDto<List<WrongQuestion>> listPageResDto = wrongQuestionService.listByUserId(reqDto, userId);

        return ApiUtils.success(listPageResDto);
    }

    /**
     * 查找下一个错题
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "查找列表")
    @RequestMapping(value = "/next", method = { RequestMethod.POST})
    public ApiResult<BaseIdRespDto> nextQu(@RequestBody WrongQuestionDto reqDto) {
        Long userId = AppUserUtils.getUser().getId();
        //转换并返回
        Long quId = wrongQuestionService.findNext(reqDto.getExamId(),reqDto.getQuestionId(),userId);
        return ApiUtils.success(new BaseIdRespDto(quId));
    }

}
